package com.example.wechatdaka.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wechatdaka.entity.UserStudent;
import com.example.wechatdaka.entity.UserWechat;
import com.example.wechatdaka.service.ClazzService;
import com.example.wechatdaka.service.UserStudentService;
import com.example.wechatdaka.utils.Result;
import com.example.wechatdaka.utils.response.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lx
 * @since 2020-11-26
 */
@Api(tags = "学生模块")
@RestController
@RequestMapping("/userstudent")
public class UserStudentController {
    @Autowired
    private UserStudentService userStudentService;
    @Autowired
    private ClazzService clazzService;
    @ApiOperation(value = "查询所有学生列表")
    @RequestMapping(value = "/index",method = RequestMethod.POST)
    public Result queryAll(){
        List<UserStudent> userStudentList = userStudentService.queryAllStudent();
        return new Result(Result.SUCCESS,userStudentList,"SUCCESS");
    }
    @ApiOperation(value = "分页查询所有学生列表")
    @RequestMapping(value = "/indexByPage",method = RequestMethod.GET)
    public JsonResult queryAllByPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                     @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize) {
        Page<UserWechat> page = new Page<>(currentPage, pagesize);
        IPage<UserStudent> userWechatIPage = userStudentService.queryAllStudentByPage(page);
        List<UserStudent> records = userWechatIPage.getRecords();
        long total = userWechatIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @ApiOperation(value = "选择学生")
    @RequestMapping(value = "/selectStudentList",method = RequestMethod.POST)
    public Result selectStudentList(){
        List<UserStudent> userStudentList = userStudentService.selectStudentList();
        return new Result(Result.SUCCESS,userStudentList,"success");
    }
    @ApiOperation(value = "添加/更新学生")
    @RequestMapping(value = "/saveOrUpdateStudent",method = RequestMethod.POST)
    public Result saveOrUpdateStudent(@RequestBody UserStudent userStudent){
        boolean saveOrUpdate = userStudentService.saveOrUpdate(userStudent);
        clazzService.updateClazzPeopleNum();
        if (saveOrUpdate){
            return new Result(Result.SUCCESS,null,"success");
        }
        return new Result(Result.ERROR,null,"ERROR");
    }
    @ApiOperation(value = "删除学生")
    @RequestMapping(value = "/deleteStudent",method = RequestMethod.POST)
    public Result deleteStudent(@RequestBody String id){
        boolean removeById = userStudentService.removeById(id);
        clazzService.updateClazzPeopleNum();
        if (removeById){
            return new Result(Result.SUCCESS,null,"success");
        }
        return new Result(Result.ERROR,null,"ERROR");
    }
    @ApiOperation(value = "分页查询班级学生列表")
    @RequestMapping(value = "/queryClazzStudentByPage",method = RequestMethod.GET)
    public JsonResult queryClazzStudentByPage(@RequestParam(required = true,defaultValue = "1",name = "currentPage")Integer currentPage,
                                     @RequestParam(required = true,defaultValue = "5",name = "pagesize")Integer pagesize,String id) {
        Page<UserWechat> page = new Page<>(currentPage, pagesize);
        IPage<UserStudent> userWechatIPage = userStudentService.queryClazzStudentByPage(page,id);
        List<UserStudent> records = userWechatIPage.getRecords();
        long total = userWechatIPage.getTotal();
        long pageNum = total / pagesize + 1;
        return JsonResult.ok().data("records",records).data("total",total).data("pageNum",pageNum);
    }
    @ApiOperation(value = "删除学生")
    @RequestMapping(value = "/queryByUserNumber",method = RequestMethod.POST)
    public Result queryByUserNumber(@RequestBody String lxUserNumber){
        UserStudent userStudent = userStudentService.queryByUserNumber(lxUserNumber);
        return new Result(Result.SUCCESS,userStudent,"success");
    }


}

